package me.ezitku.base.service


import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import me.ezitku.base.IServiceProcss
import me.ezitku.base.extensions.startService


class LocalService : Service() {
    private var binder: MyBinder? = null
    lateinit private var conn : MyServiceConnection
    lateinit var startClass:Class<*>
    override fun onBind(intent: Intent?): IBinder? {
        return binder
    }

    override fun onCreate() {
        super.onCreate()
        startClass = RemoteService::class.java
        if (binder == null) {
            binder = MyBinder()
        }
        conn = MyServiceConnection()

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        bindService(Intent(this,startClass),conn, Context.BIND_IMPORTANT)
       // sendNotification(startId)
        //startForeground(startId,createNotification("Title","Content"))

        return START_STICKY
    }


    class MyBinder : IServiceProcss.Stub() {
        override fun getProccessName(): String {
            return ""
        }

    }

    inner class MyServiceConnection : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {
            bindService(Intent(this@LocalService,startClass),conn, Context.BIND_IMPORTANT)
            startService(startClass)

        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {

        }

    }


    override fun onDestroy() {
        super.onDestroy()
        //startForeground(0,createNotification("Title","Content"))
    }

}